# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=libtomcrypt
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.18.2
pkgrel=3
pkgdesc="A fairly comprehensive, modular and portable cryptographic toolkit (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://www.libtom.net/LibTomCrypt/"
license=('custom')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-libtommath")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc")
options=('strip')
source=("https://github.com/libtom/libtomcrypt/releases/download/v${pkgver}/crypt-${pkgver}.tar.xz")
sha256sums=('96ad4c3b8336050993c5bc2cf6c057484f2b0f9f763448151567fbab5e767b84')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # Disable unnecessary warnings
  sed -e "s/ -Wsystem-headers//g" -e "s/ -Wshadow//g" -i makefile_include.mk

  # Fix linker flag
  sed -e "s/--export-all/--export-all-symbols/g" -i makefile.mingw
}

build() {
  cp -rf "${_realname}-${pkgver}" "build-${MSYSTEM}"
  cd "${srcdir}/build-${MSYSTEM}"

  make -f makefile.mingw all \
  CC=${CC} PREFIX=${MINGW_PREFIX} \
  CFLAGS="-O2 -DLTM_DESC -DGMP_DESC -DUSE_LTM" \
  LTC_LDFLAGS+=" -ltommath -lgmp"
}

check() {
  cd ${srcdir}/build-${MSYSTEM}

  ./test.exe
}

package() {
  cd ${srcdir}/build-${MSYSTEM}

  mkdir -p ${pkgdir}${MINGW_PREFIX}/include
  install -Dm755 libtomcrypt.dll "${pkgdir}${MINGW_PREFIX}/bin/libtomcrypt.dll"
  install -Dm644 libtomcrypt.a "${pkgdir}${MINGW_PREFIX}/lib/libtomcrypt.a"
  install -Dm644 libtomcrypt.dll.a "${pkgdir}${MINGW_PREFIX}/lib/libtomcrypt.dll.a"
  install -Dm644 src/headers/tomcrypt*.h "${pkgdir}${MINGW_PREFIX}/include"
  install -Dm644 doc/crypt.pdf "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/crypt.pdf"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
